package com.chao.juc.asyn.yreturn;

import java.util.concurrent.*;

/**
 * @author : Rookie
 * @description : futureTask使用
 * @date : 2023/2/4
 */
public class FutureTaskTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        withExecutor();
    }

    private static void withThread() throws ExecutionException, InterruptedException {
        FutureTask<String> futureTask = new FutureTask<>(() -> "futureTask测试！");

        new Thread(futureTask).start();

        System.out.println(futureTask.get());
    }

    private static void withExecutor() throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        FutureTask<String> futureTask = new FutureTask<>(() -> "futureTask测试！");
        executorService.execute(futureTask);
        System.out.println(futureTask.get());
        executorService.shutdown();
    }
}
